package com.example.demo.sqlInjector;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.example.demo.annotation.JoinField;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.Constants.*;

public class MySqlUtil {

    public static  void getAllSqlFromClass(Class clazz, List<String> whereSql, List<String> selectSql, List<String> joinSql, boolean isJoin){
        //获取本体的
        for (Field field : clazz.getDeclaredFields()) {
            JoinField joinField = field.getAnnotation(JoinField.class);
            if (joinField != null) {
                getAllSqlFromClass(field.getType(), whereSql, selectSql,joinSql, true);
                //生成JoinSql
                joinSql.add(getJoinFromClass(clazz,field,joinField));
            } else { //普通参数
                TableField tableField = field.getAnnotation(TableField.class);
                if(tableField!=null&&tableField.exist()==false){//隐藏的
                    continue;
                }
                whereSql.add(getWhereSqlFromClass(clazz, field, isJoin));
                selectSql.add(getSelectSqlFromClass(clazz, field,isJoin));
            }
        }
    }

    public static  String getWhereSqlFromClass(Class clazz, Field field, Boolean isJoin) {
        String whereColumn = AND + " " + unCapSimpleName(clazz) + DOT + StringUtils.camelToUnderline(field.getName());
        String wherePropertyPrefix = WRAPPER_ENTITY;
        if (isJoin) {
            wherePropertyPrefix = WRAPPER_ENTITY_DOT + unCapSimpleName(clazz);
        }
        String whereProperty = wherePropertyPrefix + DOT + field.getName();
        String sql;
        if(field.getType() == String.class){ //字符
            sql = whereColumn + " like " + String.format("concat('%%',#{%s},'%%')", whereProperty);
        } else {
            sql = whereColumn + " = " + String.format("#{%s}", whereProperty);
        }
        sql = SqlScriptUtils.convertIf(sql, String.format("%s !=null and %s !=null", wherePropertyPrefix, whereProperty), true);
        //如果有时间
        if (field.getType() == Date.class){//时间 >= Start <=End
            String sq = whereColumn + "&gt;=" + String.format("#{%sStart}", whereProperty) + NEWLINE + whereColumn + "&lt;=" + String.format("#{%sEnd}", whereProperty);
            sq = SqlScriptUtils.convertIf(sq, String.format("%s !=null and %sStart !=null  and %sEnd !=null", wherePropertyPrefix, whereProperty,whereProperty), true);
            sql+=sq;
        }

        return sql;
    }

    public static  String getSelectSqlFromClass(Class clazz, Field field,Boolean isJoin) {
        String sqlColumn = unCapSimpleName(clazz) + DOT + StringUtils.camelToUnderline(field.getName());
        String sqlProperty = field.getName();
        if(isJoin){
            sqlProperty = unCapSimpleName(clazz) + DOT + field.getName();
        }
        return String.format("%s as '%s'", sqlColumn, sqlProperty);
    }

    public static    String getJoinFromClass(Class clazz,Field field,JoinField joinField){
        //生成JoinSql
        String fk1 = joinField.fk();
        if(joinField.fk()==null||joinField.fk().equals("")){
            fk1 = StringUtils.camelToUnderline(StringUtils.camelToUnderline(field.getName()))+"_id";
        }
        fk1 = unCapSimpleName(clazz)+DOT+fk1;
        String fk2 = field.getName()+DOT+"id";
        return String.format("left join %s as %s on %s = %s ",StringUtils.camelToUnderline(field.getName()),field.getName(),fk1,fk2);
    }

    //首字母小写如UserName 转 userName
    private static String unCapSimpleName(Class clazz) {
//        return org.springframework.util.StringUtils.uncapitalize(clazz.getSimpleName());
        return StringUtils.firstToLowerCase(clazz.getSimpleName());
    }

}
